<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 订单操作日志表模型类
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_Orderlog_model extends CI_Model{

    /**
     * 更新数据
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data,$where){
        $table = $this->_get_table_name();
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        return $this->db->update($table,$data);
    }

    /**
     * 根据条件获取订单操作日志
     * @param mixed $where 查询条件
     * @param string $field 获取字段
     * @return array $data 返回数据
     */
    public function get_list($where='',$field=''){
        $table = $this->_get_table_name();
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        if(!empty($field))$this->db->select($field);
        $this->db->order_by('add_time', 'DESC');
        $query = $this->db->get($table);
        $data = array();
        $this->load->model('order.Orderinfo_model');
        $status = $this->Orderinfo_model->_set_order_status_name();
        foreach($query->result_array() as $row){
            $row['order_status_name'] = $status['order_status'][$row['order_status']];
            $row['pay_status_name'] = $status['pay_status'][$row['pay_status']];
            $data[] = $row;
        }
        return $data;
    }

    /**
     * 添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data){
        $table = $this->_get_table_name();
        if(is_array($data)){
            if($this->db->insert($table,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 查询单条记录
     * @param mixed $where 查询条件 array|string
     * @param string $field 获取到的字段
     * @return mixed 返回数据
     */
    public function get_row($where,$field='*'){
        $table = $this->_get_table_name();
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        if(!empty($field)) $this->db->select($field);
        $query = $this->db->get($table,1);
        return $query->row_array();
    }

    /**
     * 删除内容
     * @param mixed $where 查询条件 array|string
     * @return bool 返回数据
     */
    public function remove_row($where){
        $table = $this->_get_table_name();
        if(!empty($where))return $this->db->delete($table,$where);
        return false;
    }

    /**
     * 获取数据表名称
     */
    private function _get_table_name(){
        $table = $this->db->dbprefix("order_log");
        return $table;
    }
}